<?php

namespace App\Kuafu\Shop\Jobs;

use App\Kuafu\Common\Jobs\SensorsDataJob;
use App\Kuafu\Common\Service\CommonService;
use App\Kuafu\Order\Enum\CodeEnum;
use App\Kuafu\Order\Model\OrderCouponSplitModel;
use App\Kuafu\Order\Model\OrderDetailSplitModel;
use App\Kuafu\Order\Model\OrderExternalSplitModel;
use App\Kuafu\Order\Model\OrderSplitModel;
use App\Kuafu\Shop\Enum\CameraCacheEnum;
use App\Kuafu\Shop\Service\ShopCameraSyncService;
use App\Kuafu\Shop\Service\ShopService;
use App\Library\FeiShu\SendFs;
use Exception;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;
use PHP_CodeSniffer\Util\Cache;


/**
 * @description 同步门店摄像头
 *
 * @queueName {PAY_ORDER_QUEUE}
 */
class SyncCameraJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
     * 队列名称
     *
     * @var string
     */
    public static string $QUEUE_NAME = '{RMS-SYNC-CAMERA-QUEUE}';

    /**
     * 门店id
     *
     * @var int
     */
    public int $shopId;


    /**
     * 订单id
     *
     * @var int
     */
    public int $orderId;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($shopId)
    {
        $this->shopId = $shopId;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $server   = new ShopCameraSyncService();
        $shopInfo = ShopService::getShopInfo($this->shopId);

        $count = $server->syncYlkCamera($this->shopId, $shopInfo->cameraSupplierId);
        if ($count) {
            $sendFs = SendFs::newRobot(SendFs::ROBOT_BUSINESS_WARING)
                            ->setTitleContent(date('Y-m-d H:i:s') . ' 门店 ' . $shopInfo->name . ' 悠络客更新成功',
                                '更新了 ' . $count . ' 台设备 . ');

            if ($count > 5) {
                $sendFs->setAtUid('3fgc8798');
                $sendFs->setAtUid('2c54915f');
//                $sendFs->setAtUid('fea66474');
            }

            $sendFs->send();
        }

        // 解锁
        $redis_key = 'syncYlkCamera:' . $this->shopId;
        \Illuminate\Support\Facades\Cache::forget(CameraCacheEnum::getCameraListCacheKey($this->shopId));
        Redis::del($redis_key);
    }

}
